<template>
	<a-form :ref="'ruleForm'" :model="formState" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
		<a-row :gutter="[10, 10]">
			<a-col :span="24">
				<a-radio-group
					v-model:value="formState.dataSetConfig.dataSetType"
					optionType="button"
					buttonStyle="solid"
					:options="[
						{ label: '静态', value: 0 },
						{ label: '数据源', value: 1 }
					]"
				>
				</a-radio-group>
			</a-col>
			<template v-if="formState.dataSetConfig.dataSetType == 0">
				<a-col :span="24">
					<a-form-item label="标题" name="title">
						<a-input v-model:value="formState.data.title.value" />
					</a-form-item>
					<a-form-item label="数值" name="numValue">
						<a-input v-model:value="formState.data.numValue.value" />
					</a-form-item>
				</a-col>
			</template>
			<template v-else>
				<a-col :span="24">
					<SelectTree @onChange="selectTreeChange" v-model:paramsJson="formState.dataSetConfig.dataSetJson" />
				</a-col>
				<a-col :span="24">
					<a-form-item label="展示字段">
						<a-select
							style="width: 100%"
							placeholder="请选择展示字段"
							v-model:value="formState.data.title.field"
							@change="selectDataSet"
							optionLabelProp="label"
						>
							<a-select-option
								v-for="optionItem in dataOption"
								:value="optionItem"
								:key="optionItem"
								:label="optionItem"
							>
								{{ optionItem }}
							</a-select-option>
						</a-select>
						<a-input
							v-if="formState.data.title.field"
							class="mt-[10px]"
							v-model:value="formState.data.title.alias"
							placeholder="字段别名"
						>
							<template #addonBefore>
								<div class="field-prefix">
									<a-tag color="blue">{{ formState.data.title.field }}</a-tag>
								</div>
							</template>
						</a-input>
					</a-form-item>
				</a-col>
			</template>
		</a-row>
	</a-form>
</template>

<script setup>
	import { ref, watch, computed } from 'vue'
	import SelectTree from '@/views/plugin/dashboard/plugin/public/SelectTree/SelectTree.vue'
	import config from '../config'
	import { reactive, defineProps, defineEmits } from 'vue'
	import datasetApi from '@/views/plugin/dashboard/api/datasetApi'
	import { designStore } from '@/views/plugin/dashboard/store'
	import { cloneDeep } from 'lodash-es'
	const useDesignStore = designStore()
	const emits = defineEmits(['upDataConfig'])
	const props = defineProps({
		config: {
			type: Object,
			default: () => {
				return config
			}
		}
	})
	// 数据源
	const dataSource = ref([])
	// 模拟接口返回下拉数据
	const dataOption = ref([])
	// const formState = reactive({
	// 	data: props.config.data,
	// 	dataSetConfig: props.config.dataSetConfig
	// })

	// 静态
	const staticData =
		props.config.dataSetConfig.dataSetType === 1 ? ref(cloneDeep(config.data)) : ref(cloneDeep(props.config.data))
	// 动态
	const dynamicData =
		props.config.dataSetConfig.dataSetType === 0 ? ref(cloneDeep(config.data)) : ref(cloneDeep(props.config.data))

	const formState = computed({
		get() {
			const dataSetConfig = props.config.dataSetConfig // 更新的状态
			// 初始值
			const dataSource = dataSetConfig.dataSetType === 1 ? dynamicData.value : staticData.value
			return {
				data: dataSource,
				dataSetConfig: dataSetConfig
			}
		},
		set(value) {
			console.log(value, 'value')
			if (value.dataSetConfig.dataSetType === 1) {
				dynamicData.value = value.data
			} else {
				staticData.value = value.data
			}
		}
	})
	// 数据变化清空下拉
	const selectTreeChange = () => {
		formState.value.data.title.field = undefined
		formState.value.data.title.alias = undefined
	}
	// 获取下拉数据
	watch(
		() => formState.value.dataSetConfig.dataSetJson,
		(newVal, oldVal) => {
			if (props.config.dataSetConfig.dataSetType === 1) {
				const list = useDesignStore.dataSource || []
				let datasetId = ''
				let indicatorId = ''
				list.forEach((parent) => {
					parent.config.indicators.forEach((child) => {
						if (child.id === newVal.id) {
							datasetId = parent.id
							indicatorId = child.id
						}
					})
				})
				if (datasetId && indicatorId) {
					// 下拉数据
					datasetApi.querySelectedColumns({ datasetId, indicatorId }).then((res) => {
						dataOption.value = res || []
					})
					datasetApi.preview(newVal).then((res) => {
						dataSource.value = res || []
					})
				}
			}
		},
		{
			immediate: true,
			deep: true
		}
	)
	// 选择数据
	const selectDataSet = (val) => {
		formState.value.data.title.field = val
		formState.value.data.numValue.value = dataSource.value[0][val]
	}

	// 更新组件内容
	watch(
		() => formState.value,
		(newVal) => {
			formState.value = newVal
			emits('upDataConfig', { ...props.config, ...newVal })
		},
		{
			deep: true
		}
	)
</script>
<style scoped>
	:deep(.ant-radio-button-wrapper) {
		padding: 0px;
		width: 100%;
	}

	:deep(.ant-radio-group) {
		display: flex;
		text-align: center;
	}

	.alias-input-container {
		display: flex;
		flex-direction: column;
		gap: 8px;
	}

	.field-prefix {
		display: flex;
		align-items: center;
		white-space: nowrap;
		margin-right: -12px;
	}

	:deep(.ant-input-group-addon) {
		background-color: #fafafa;
		padding: 0 8px;
	}

	.field-preview {
		background-color: #f5f5f5;
		border-radius: 2px;
		padding: 8px 12px;
		margin-top: 8px;
	}

	.preview-title {
		font-size: 12px;
		color: #666;
		margin-bottom: 4px;
	}

	.preview-content {
		display: flex;
		flex-direction: column;
		gap: 4px;
	}

	.field-name {
		font-weight: 500;
		color: #1677ff;
	}

	.field-value {
		font-size: 20px;
		font-weight: bold;
		color: #333;
	}
	:deep(.form-item) {
		margin-bottom: 0px;
	}
</style>
